using System;
using System.Collections;
using System.Web;
using System.Web.UI.WebControls;

using Deepcode.Flux.Core.Services.XML;
using Deepcode.Flux.Core.Systems.Workflow.Bases;
using Deepcode.Flux.Core.Systems.Workflow.Interfaces;

namespace Deepcode.Flux.Core.Systems.Workflow.BaseHandlers
{
	/// <summary>
	/// Exposes the configuration form for the send an email action
	/// </summary>
	public class SimpleSendAnEmailConfig : IWorkflowHandlerMethodConfig
	{
		/// <summary>
		/// Will hold the controls for the options
		/// </summary>
		ArrayList controls = new ArrayList();
		
		// Control declarations
		Deepcode.Flux.Core.UI.Controls.ShadePadBox spb;
		System.Web.UI.WebControls.Table ContainerTable;
		System.Web.UI.WebControls.TableRow ToRow;
		System.Web.UI.WebControls.TableCell ToCaptionCell;
		System.Web.UI.WebControls.TableCell ToDataCell;
		System.Web.UI.WebControls.TextBox ToField;
		
		System.Web.UI.WebControls.TableRow SubjectRow;
		System.Web.UI.WebControls.TableCell SubjectCaptionCell;
		System.Web.UI.WebControls.TableCell SubjectDataCell;
		System.Web.UI.WebControls.TextBox SubjectField;

		System.Web.UI.WebControls.TableRow BodyCaptionRow;
		System.Web.UI.WebControls.TableCell BodyCaptionCell;
		
		System.Web.UI.WebControls.TableRow BodyDataRow;
		System.Web.UI.WebControls.TableCell BodyDataCell;
		System.Web.UI.WebControls.TextBox BodyField;
		/// <summary>
		/// Build the configuration form
		/// </summary>
		/// <param name="HandlerID"></param>
		/// <param name="DefinitionID"></param>
		/// <param name="ProcessID"></param>
		/// <returns></returns>
		public System.Collections.ArrayList AddFormControls(System.Web.UI.Page CallingPage, string HandlerID, int DefinitionID, int ProcessID)
		{
			controls.Clear();
			// Setup a shade pad box
			spb = new Deepcode.Flux.Core.UI.Controls.ShadePadBox();

			// Setup table
			ContainerTable = new System.Web.UI.WebControls.Table();
			ContainerTable.CellSpacing = 0;
			ContainerTable.CellPadding = 5;
			ContainerTable.Width = new Unit("100%");

			spb.Controls.Add( ContainerTable );

			// TO: Field row
			ToRow = new System.Web.UI.WebControls.TableRow();
			ToCaptionCell = new System.Web.UI.WebControls.TableCell();
			ToCaptionCell.Text = "To:";
			ToCaptionCell.Wrap = false;

			ToDataCell = new System.Web.UI.WebControls.TableCell();
			ToDataCell.Width = new Unit("100%");
			ToField = new System.Web.UI.WebControls.TextBox();
			ToField.Width = new Unit("100%");
			
			ContainerTable.Controls.Add( ToRow );
			ToRow.Controls.Add( ToCaptionCell );
			ToRow.Controls.Add( ToDataCell );
			ToDataCell.Controls.Add( ToField );
			
			// SUBJECT: Field row
			SubjectRow = new System.Web.UI.WebControls.TableRow();
			SubjectCaptionCell = new System.Web.UI.WebControls.TableCell();
			SubjectCaptionCell.Text = "Subject:";
			SubjectCaptionCell.Wrap = false;

			SubjectDataCell = new System.Web.UI.WebControls.TableCell();
			SubjectField = new System.Web.UI.WebControls.TextBox();
			SubjectField.Width = new Unit("100%");

			ContainerTable.Controls.Add( SubjectRow );
			SubjectRow.Controls.Add( SubjectCaptionCell );
			SubjectRow.Controls.Add( SubjectDataCell );
			SubjectDataCell.Controls.Add( SubjectField );

			// EMAIL BODY: Title row
			BodyCaptionRow = new System.Web.UI.WebControls.TableRow();
			BodyCaptionCell = new System.Web.UI.WebControls.TableCell();
			BodyCaptionCell.Text = "Email Body:";
			BodyCaptionCell.ColumnSpan = 2;
			
			ContainerTable.Controls.Add( BodyCaptionRow );
			BodyCaptionRow.Controls.Add( BodyCaptionCell );

			// EMAIL BODY: Field row
			BodyDataRow = new System.Web.UI.WebControls.TableRow();
			BodyDataCell = new System.Web.UI.WebControls.TableCell();
			BodyDataCell.ColumnSpan = 2;
			BodyField = new System.Web.UI.WebControls.TextBox();
			BodyField.Width = new Unit("100%");
			BodyField.Height = new Unit("300px");
			BodyField.TextMode = System.Web.UI.WebControls.TextBoxMode.MultiLine;

			ContainerTable.Controls.Add( BodyDataRow );
			BodyDataRow.Controls.Add( BodyDataCell );
			BodyDataCell.Controls.Add( BodyField );

			// Add the Table to the control array
			controls.Add( spb );
			return controls;
		}
		/// <summary>
		/// Serialise the configuration form to XML
		/// </summary>
		/// <returns></returns>
		public string Serialise()
		{
			PropertyBag pb = new PropertyBag();
			pb.Add("ToEmail", ToField.Text);
			pb.Add("SubjectLine", SubjectField.Text);
			pb.Add("Body", BodyField.Text);
			return pb.Serialise();
		}
		/// <summary>
		/// Deserialise the XML to the configuration form
		/// </summary>
		/// <param name="XmlDatapacket"></param>
		public void DeSerialise(string XmlDatapacket)
		{
			PropertyBag pb = new PropertyBag(XmlDatapacket);
			ToField.Text = pb.Get("ToEmail", "");
			SubjectField.Text = pb.Get("SubjectLine", "");
			BodyField.Text = pb.Get("Body", "");
		}
	}
}
